package com.yusp75.myexam.com.yusp75.myexam.di

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

class StateFlowManager {
    internal val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)

    val counterFlow = MutableStateFlow(0).also { flow ->
        scope.launch {
            flow.collect { value ->
                println("Received: $value")
            }
        }
    }

    fun destroy() {
        scope.cancel() // 必须手动取消防止内存泄漏
    }
}
